Skip to main content

Matrix Transpose

This is an example of taking the transpose of a matrix.

from csdl_om import Simulatorfrom csdl import Modelimport csdlimport numpy as np

class ExampleMatrix(Model):
    def define(self):
        # Declare mat as an input matrix with shape = (4, 2)        mat = self.declare_variable(            'Mat',            val=np.arange(4 * 2).reshape((4, 2)),        )
        # Compute the transpose of mat        self.register_output('matrix_transpose', csdl.transpose(mat))

sim = Simulator(ExampleMatrix())sim.run()
print('Mat', sim['Mat'].shape)print(sim['Mat'])print('matrix_transpose', sim['matrix_transpose'].shape)print(sim['matrix_transpose'])
[[0. 1.] [2. 3.] [4. 5.] [6. 7.]]matrix_transpose (2, 4)[[0. 2. 4. 6.] [1. 3. 5. 7.]]